<?php

namespace app\admin\controller\home;

use app\common\controller\Backend;
use function fast\e;
use think\Db;

/**
 * 专家排班
 *
 * @icon fa fa-circle-o
 */
class Working extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Working;
        $this->model = new \app\admin\model\Book();
    }

    /**
     * 查看
     */
    public function index()
    {

        //获取本周日期
        $week = get_week(time());
        foreach ($week as $k=>$v){
            $week[$k]['str'] = $v['date'].'('.$v['week'].')';
        }

        $data = [
            1 => [
                $week,
            ],
            2 => [
                $week,
            ],
            3 => [
                $week,
            ],
            4 => [
                $week,
            ],
            5 => [
                $week,
            ],
            6 => [
                $week,
            ],
        ];

        foreach ($data as $k=>$v){
            foreach ($v as $kk=>$vv){
                foreach ($vv as $kkk=>$vvv){
                    $datas = Db::table('fa_working')
                        ->alias('a')
                        ->field('a.id,b.name,a.medic_id')
                        ->join('fa_medic b','a.medic_id=b.id','left')
                        ->where(['a.date'=>$vvv['date'],'a.time'=>$k])
                        ->select();

                    if (!empty($datas)){
                        $names = [];
                        $ids = [];
                        $idss = [];
                        foreach ($datas as $ks=>$vl){
                            $names[$ks] = $vl['name'];
                            $ids[$ks] = $vl['medic_id'];
                            $idss[$vl['medic_id']] = $vl['id'];
                        }

                        $data[$k][$kk][$kkk]['name'] = implode(',',$names);
                        $data[$k][$kk][$kkk]['ids'] = implode(',',$ids);
                        $data[$k][$kk][$kkk]['idss'] = json_encode($idss);
                    }else{
                        $data[$k][$kk][$kkk]['name'] = '';
                        $data[$k][$kk][$kkk]['ids'] = '';
                        $data[$k][$kk][$kkk]['idss'] = '';
                    }

                }

            }

        }


        //var_dump($data);die();

        $this->view->assign("data", $data);
        $this->view->assign("week", $week);
        $this->view->assign("time", time());
        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = Db::table('fa_medic')
            ->where(['status'=>1,'type'=>'专家'])
            ->select();

        $data = [
            'ids' => request()->param()['ids'],
            'time' => request()->param()['time'],
            'date' => request()->param()['date'],
        ];

        $idss = json_decode(urldecode(request()->param()['idss']),true);

        $ids_ar = explode(',',$data['ids']);

        if (empty($row)) {
            $this->error(__('没有医护人员,不能进行排班'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if (empty($params['pid'])){
                $this->error(__('专家人员不能为空'));
            }

            //判断排班时间段是否过期
            switch ($params['time']){
                case 1:
                    if (date('Y-m-d H:i:s') > $params['date'].' 10:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
                case 2:
                    if (date('Y-m-d H:i:s') > $params['date'].' 11:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
                case 3:
                    if (date('Y-m-d H:i:s') > $params['date'].' 12:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
                case 4:
                    if (date('Y-m-d H:i:s') > $params['date'].' 15:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
                case 5:
                    if (date('Y-m-d H:i:s') > $params['date'].' 16:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
                case 6:
                    if (date('Y-m-d H:i:s') > $params['date'].' 17:00:00'){
                        $this->error('预约时间已超时');
                    }
                    break;
            }

            $del = [];
            $insert = [];


            if ($params) {
                $params = $this->preExcludeFields($params);

                foreach ($ids_ar as $k=>$v){
                    if (!in_array($v,$params['pid'])){
                        $del[] = $idss[$v];
                    }
                }

                foreach ($params['pid'] as $k=>$v){
                    if (!in_array($v,$ids_ar)){
                        $insert[] = $v;
                    }
                }


                if (!empty($del)){

                    foreach ($del as $kk=>$vv){
                        $pan_arr = Db::table('fa_registration')
                            ->alias('a')
                            ->field('a.id,b.name')
                            ->join('fa_medic b','a.medic_id=b.id','left')
                            ->where('a.working_id',$vv)
                            ->find();

                        if (!empty($pan_arr)){
                            $this->error(__($pan_arr['name'].',该专家已有预约记录,不能删除'));
                        }
                    }

                    foreach ($del as $k=>$v){
                        Db::table('fa_working')
                            ->where('id',$v)
                            ->delete();
                    }
                }


                if (!empty($insert)){
                    foreach ($insert as $k=>$v){
                        Db::table('fa_working')
                            ->insertGetId([
                                'medic_id' => $v,
                                'date' => $params['date'],
                                'time' => $params['time'],
                                'create_time' => time(),
                            ]);
                    }
                }

                $this->success('成功');

            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        $this->view->assign("data", $data);
        $this->view->assign("ids_ar", $ids_ar);
        return $this->view->fetch();
    }


}
